<template>
  <fks-dialog
    :title="LANG.PROCESS_PROCESSING"
    :visible="visible"
    @open="openHandler"
    @close="close"
  >
    <fks-form
      ref="Form"
      :model="Form"
      labfks-width="100px"
      labfks-position="left"
    >
      <fks-row :gutter="32">
        <fks-col :span="12" style="white-space: nowrap">
          <fks-form-item required :label="LANG.RETURN_NODE" prop="targetKey">
            <fks-select v-model="Form.targetKey" style="width: 100%">
              <fks-option
                v-for="item in taskNodeList"
                :key="item.taskKey"
                :label="item.taskName"
                :value="item.taskKey"
              />
            </fks-select>
          </fks-form-item>
        </fks-col>
      </fks-row>
    </fks-form>
    <div slot="footer" style="text-align: right">
      <fks-button type="primary" plain @click="close()">
        {{ LANG.CANCEL }}
      </fks-button>
      <fks-button type="primary" @click="submit">
        {{ LANG.CONFIRM }}
      </fks-button>
    </div>
  </fks-dialog>
</template>

<script>
import { getTaskNode, rejectFlow } from '../api.js'
export default {
  name: 'FlowManageDetail',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'add'
    },
    taskId: {
      type: String,
      default: ''
    },
    taskKey: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      Form: {
        targetKey: ''
      },
      taskNodeList: []
    }
  },
  methods: {
    submit() {
      this.$refs.Form.validate((validata) => {
        if (validata) {
          const data = {
            taskId: this.taskId,
            targetKey: this.Form.targetKey,
            comment: ''
          }
          rejectFlow(data).then((res) => {
            if (res.status) {
              this.$message({
                type: 'success',
                message: this.LANG.EDIT_SUCCESS
              })
              this.$parent.getData()
              this.close()
            }
          })
        }
      })
    },
    openHandler() {
      if (this.taskId) {
        getTaskNode(this.taskId).then((res) => {
          if (res.status) {
            let nowNodeIndex = res.data.findIndex(
              (item) => item.taskKey == this.taskKey
            )
            if (this.taskKey !== 'fawkes_custom_flow_start') {
              this.taskNodeList = res.data.slice(0, nowNodeIndex)
            }
          }
        })
      }
    },
    close() {
      this.taskNodeList = []
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style>
</style>
